<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/23 2:15
 */

namespace app\common\business;


use think\Exception;
use think\facade\Log;

class BusBase {

    /**
     * 新增逻辑
     * @param $data
     * @return bool|mixed
     */
    public function add($data){
        $data['status'] = isset($data['status'])||!empty($data['status']) ? $data['status'] : config('status.mysql.table_normal');
        try {
            $this->model->save($data);
        } catch (\Exception $e) {
            Log::error('common-business-busBase-add : ' . $e->getMessage());
            return 0;
        }
        return $this->model->id;
    }

    /**
     * 更新逻辑
     * @param $id
     * @param $data
     * @return int
     */
    public function edit($id, $data){
        try {
            unset($data['id']);
            unset($data['__token__']);
            $this->model->updateById($id, $data);
        } catch (\Exception $e) {
            Log::error('common-business-busBase-edit : ' . $e->getMessage());
            return 0;
        }
        return true;
    }

    /**
     * 根据 id 获取单条数据
     * @param $id
     * @return array|bool|\think\Model|null
     */
    public function getById($id) {
        try {
            $info = $this->model->getDataInfoById($id);
            if (!$info) {
                return [];
            }
        } catch (\Exception $e) {
            Log::error('common-business-busBase-getById: ' . $e->getMessage());
            return false;
        }
        return $info->toArray();
    }

    /**
     * 根据 ids 获取多条数据
     * @param $ids
     * @return array|bool|\think\Model|null
     */
    public function getByIds($ids) {
        try {
            $result = $this->model->getDataByIds($ids);
            if (!$result) {
                return [];
            }
        } catch (\Exception $e) {
            Log::error('common-business-busBase-getByIds: ' . $e->getMessage());
            return false;
        }
        return $result->toArray();
    }

    /**
     * 更改状态 / 排序 / 根据字段传入修改
     * @param $id
     * @param $value
     * @param string $field
     * @return bool
     * @throws Exception
     */
    public function setFiledToValue($id, $value, $field = 'status') {
        $res = $this->model->getById($id);
        if (!$res) {
            throw new Exception('不存在该条记录');
        }
        $data = [
            $field => trim($value),
        ];
        try {
            $res = $this->model->updateById($id, $data);
        } catch (\Exception $e) {
            // 记录日志
            Log::error('common-business-busBase-setFiledToValue:' .$e->getMessage());
            return false;
        }
        return $res;
    }

    /**
     * 根据条件获取单数据
     * @param array $condition
     * @return bool
     */
    public function getByConditionFind($condition = []) {
        try {
            $result = $this->model->getByConditionFind($condition);
            if (!$result) {
                return [];
            }
        } catch (\Exception $e) {
            // 记录日志
            Log::error('common-business-busBase-getByConditionFind:' .$e->getMessage());
            return false;
        }
        return $result->toArray();
    }

    /**
     * 根据条件获取多数据
     * @param array $condition
     * @param array $order
     * @return bool
     */
    public function getByCondition($condition = [], $order = []) {
        try {
            $result = $this->model->getByCondition($condition, $order);
            if (!$result) {
                return [];
            }
        } catch (\Exception $e) {
            // 记录日志
            Log::error('common-business-busBase-getByCondition:' .$e->getMessage());
            return false;
        }
        return $result->toArray();
    }

}